import torch
import pandas as pd
from model import RecommenderModel
import config

class Recommend:
    def __init__(self):
        self.model = RecommenderModel(config.model_input_size, config.model_hidden_size, config.model_output_size)
        self.model.load_state_dict(torch.load('model.pth'))
        self.recommend_data = pd.read_csv('recommend.csv')

    def predict_rating(self, userId, movieId):
        # 假设有一个函数get_features_from_ids可以根据userId和movieId获取特征
        # input_data = get_features_from_ids(userId, movieId)
        inputs = self.recommend_data[['userId', 'movieId']].values

        input_tensor = torch.tensor(inputs, dtype=torch.float32)  # 将输入张量转换为 float32 类型

        with torch.no_grad():
            output = self.model(input_tensor)

        return output.item()  # 返回预测的评分

# 创建 recommend 实例
recommender = Recommend()

# 使用 recommend 实例进行预测
predicted_rating = recommender.predict_rating(1, 3)
print(predicted_rating)  # 打印预测的评分
